package wayden;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mysql.jdbc.Statement;
import com.opensymphony.xwork2.ActionContext;

class Messages {
    
    private String text;
    private String from;
    
    public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public Messages(String text) {
	super();
	this.text = text;
	
    }
    
    public Messages(String text, String from) {
    	super();
    	this.text = text;
    	this.from = from;
        }
    
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
    
    

    
    
}

public class Message {
    private String tempId;
    Map session;
    public String getTempId() {
        return tempId;
    }

    public void setTempId(String tempId) {
        this.tempId = tempId;
    }
    private String sms;
    private String id;
    private String id2;
    private String name;
    List aa = new ArrayList < Messages >();
    
    public void setAa() {

	this.aa.add("a");

    }

    public List getAa() {
	return this.aa;
    }
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSms() {
	return sms;
    }
    
    public void setSms(String sms) {
	this.sms = sms;
    }

    public String getId2() {
	return id2;
    }

    public void setId2(String id) {
	this.id2 = id;
    }

    public String sendMessage() throws ClassNotFoundException, SQLException{
    session = ActionContext.getContext().getSession();
    if (session.get("user") != null) {
    String select = "select * from users where id = ?";
	ConnectDB connectDB = new ConnectDB();
	Connection conn = null;
	Statement st = null;
	conn = connectDB.Connect();
	PreparedStatement ps = conn.prepareStatement(select);
	ps.setString(1, getId2());
	ps.execute();
	ResultSet rs = ps.getResultSet();
	if (rs.next()) {
	    this.name = rs.getString(4);
	}
	
	
	
	
	
	return "success";
	
    }
    else return "error";
    }
    
    public String send() throws ClassNotFoundException, SQLException {
    session = ActionContext.getContext().getSession();
	String insert = "insert into message (id_user_in, id_user_out, text, new)" +
			"values (?,?,?,?)";
	ConnectDB connectDB = new ConnectDB();
	Connection conn = null;
	setId(session.get("user").toString());
	conn = connectDB.Connect();
	PreparedStatement ps = conn.prepareStatement(insert);
	ps.setString(1, getId2());
	ps.setString(2, getId());
	ps.setString(3, getSms());
	ps.setString(4, "1");
	ps.execute();
	
	
	
	System.err.println(getSms());
	System.err.println(getId2());
	System.err.println(getId());
	//view();
	setTempId(getId());
	return "success";
    }
    
    public String messages() throws SQLException, ClassNotFoundException {
    session = ActionContext.getContext().getSession();
	//System.err.println(getTempId());
    if (session.get("user")!=null) {
    	ResultSet rs2;
	Message m = new Message();
	String select = "select * from message where id_user_in = ? or id_user_out = ?";
	String user = "select * from users where id = ?";
	ConnectDB connectDB = new ConnectDB();
	Connection conn = null;
	Statement st = null;
	conn = connectDB.Connect();
	PreparedStatement ps = conn.prepareStatement(select);
	setId(session.get("user").toString());
	ps.setString(1, getId());
	ps.setString(2, getId());
	
	ps.execute();
	ResultSet rs = ps.getResultSet();
	while (rs.next()) {
	    System.err.println(rs.getString(4));
	    this.name = rs.getString(4);
	    if (!rs.getString(2).equals(getId())) {
	    	aa.add(new Messages(rs.getString(4)));
	    	System.err.println("a");
	    }
	    
	    else {
		ps = conn.prepareStatement(user);
		ps.setString(1, rs.getString(3));
		ps.execute();
		rs2 = ps.getResultSet();
		rs2.next();
	    	aa.add(new Messages(rs.getString(4), rs2.getString(4)));
	    	System.err.println("b");
	    }
	    	
	    System.err.println(rs.getString(4));
	    
	    
	}
	

	return "success";
    }
    else return "error";
    }


}
